// 滑块验证码组件
<template>
  <div ref="dragDiv" class="drag">
    <div class="drag_bg" />
    <div class="drag_text">{{ confirmWords }}</div>
    <div
      ref="moveDiv"
      class="handler"
      style="position: absolute; top: 0px; left: 0px"
      @mousedown="mouseDownFn($event)"
    > ||| </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      beginClientX: 0, // 距离屏幕左端距离,
      mouseMoveState: false, // 触发拖动状态  判断
      maxWidth: '', // 拖动最大宽度，依据滑块宽度算出来的
      confirmWords: '请按住滑块，拖动到最右边',
      confirmSuccess: false // 验证成功判断,
    }
  },
  mounted() {
    this.maxWidth =
      this.$refs.dragDiv.clientWidth - this.$refs.moveDiv.clientWidth
    document
      .getElementsByTagName('html')[0]
      .addEventListener('mousemove', this.mouseMoveFn)
    document
      .getElementsByTagName('html')[0]
      .addEventListener('mouseup', this.moseUpFn)
  },
  methods: {
    // mousedown 事件
    mouseDownFn: function(e) {
      if (!this.confirmSuccess) {
        e.preventDefault && e.preventDefault() // 阻止文字选中等 浏览器默认事件
        this.mouseMoveState = true
        this.beginClientX = e.clientX
      }
    },
    // 验证成功函数
    successFunction() {
      this.confirmSuccess = true
      this.$emit('verify_status', this.confirmSuccess)
      this.confirmWords = '验证通过'
      if (window.addEventListener) {
        document
          .getElementsByTagName('html')[0]
          .removeEventListener('mousemove', this.mouseMoveFn)
        document
          .getElementsByTagName('html')[0]
          .removeEventListener('mouseup', this.moseUpFn)
      } else {
        document
          .getElementsByTagName('html')[0]
          .removeEventListener('mouseup', () => {})
      }
      document.getElementsByClassName('drag_text')[0].style.color = '#fff'
      document.getElementsByClassName('handler')[0].style.left =
        this.maxWidth + 'px'
      document.getElementsByClassName('drag_bg')[0].style.width =
        this.maxWidth + 28 + 'px'
    },
    // mousemove事件
    mouseMoveFn(e) {
      if (this.mouseMoveState) {
        const width = e.clientX - this.beginClientX
        if (width > 0 && width <= this.maxWidth) {
          document.getElementsByClassName('handler')[0].style.left =
            width + 'px'
          document.getElementsByClassName('drag_bg')[0].style.width =
            width + 28 + 'px'
        } else if (width > this.maxWidth) {
          this.successFunction()
        }
      }
    },
    // mouseup事件
    moseUpFn(e) {
      this.mouseMoveState = false
      var width = e.clientX - this.beginClientX
      if (width < this.maxWidth) {
        document.getElementsByClassName('handler')[0].style.left = 0 + 'px'
        document.getElementsByClassName('drag_bg')[0].style.width = 0 + 'px'
      }
    }
  }
}
</script>
<style scoped>
.drag {
  position: relative;
  background-color: #ebebeb;
  width: 100%;
  height: 42px;
  line-height: 42px;
  text-align: center;
  border: 0px solid #ccc;
  border-radius: 21px;
}
.handler {
  width: 69px;
  height: 42px;
  line-height: 40px;
  text-align: center;
  letter-spacing: 5px;
  color: #DEDEDE;
  border: 2px solid #DEDEDE;
  border-radius: 21px;
  cursor: move;
  background-color: #fff;
   /* 禁止选中 */
  -webkit-user-select:none;
  -moz-user-select:none;
  -ms-user-select:none;
  user-select:none;
}
.drag_bg {
  background-color: #62C992;
  height: 42px;
  width: 0px;
  border-radius: 21px 0 0 21px;
}
.drag_text {
  color: #B3B3B3;
  position: absolute;
  top: 0px;
  width: 100%;
  text-align: center;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  -o-user-select: none;
  -ms-user-select: none;
}
</style>
